<template>
  <!-- 摄像头列表组件 -->
  <div class="allcameramain">
    <!-- 摄像头列表 -->
    <div class="cameralistmain">
      <div class="camera-title">选择摄像头</div>
      <div v-if="cameraList[0]" class="camera-zlist">
        <div v-for="(item, index) in cameraList" :key="index" class="camera-item" :title="item.cameraName" :class="{
          beselect: cameraId === item.iotCameraId,
        }" @click="getcamermes(item.iotCameraId); controlFlag = item.controlFlag">
          <i class="el-icon-video-camera"></i>
          {{ item.cameraName }}
        </div>
      </div>
      <!-- 判断有没有摄像头数据 -->
      <div v-if="!cameraList[0]" class="camera-zlist zlistbtn">
        <div class="camera-item btn">暂无摄像头</div>
      </div>
    </div>
    <!-- 电子巡塘拍照 -->
    <div class="camera-module" v-show="cameraList.length > 0 && cameraId">
      <span class="title">巡塘拍照：</span>
      <el-button @click="queryPhotograph(true)" type="primary" plain>正常</el-button>
      <el-button @click="queryPhotograph(false)" type="danger" plain>异常</el-button>
    </div>
    <div v-if="!(cameraList.length > 0 && cameraInfoList && judgePermission(3, 'control'))" class="shade"></div>
    <div v-if="!controlFlag" class="shade1"></div>
    <!-- 摄像头控制按钮 -->
    <div class="cameracontrol">
      <div class="control">
        <div class="keyList">
          <div class="contro-m z-top" @click="startcontrol(0)">
            <i class="el-icon-caret-top"></i>
          </div>
          <div class="contro-m z-left" @click="startcontrol(2)">
            <i class="el-icon-caret-left"></i>
          </div>
          <div class="contro-m z-right" @click="startcontrol(3)">
            <i class="el-icon-caret-right"></i>
          </div>
          <div class="contro-m z-bottom" @click="startcontrol(1)">
            <i class="el-icon-caret-bottom"></i>
          </div>
          <div class="contro-m z-mirror" @click="mirrorcontrol()">
            <i class="el-icon-refresh-right"></i>
          </div>
        </div>
        <div class="focal-length">
          <div class="left" @click="startcontrol(10)">焦距+</div>
          <div class="right" @click="startcontrol(11)">焦距-</div>
        </div>
      </div>
      <div style="text-align: center;margin-top: 20px; font-size: 18px;">
        <el-button @click="startcontrol(8)" type="primary" icon="el-icon-circle-plus-outline"><span
            style="font-size:18px ;">放大</span></el-button>
        <el-button @click="startcontrol(9)" type="warning" icon="el-icon-remove-outline"><span
            style="font-size:18px ;">缩小</span></el-button>
      </div>
    </div>
    <CameraModule :show="showCameraBox" :cameraInfo="cameraInfo" @closeDialog="closeDialog"></CameraModule>
  </div>
</template>
<script>
import VideoSurveillanceApi from "@/api/iot-monitoring/video-surveillance";
import inspectionElectronicPatrolApi from "@/api/inspection-management/inspection-electronicPatrol"
import CameraModule from "./cameraModule.vue";
import { mapGetters } from "vuex";
import { timestampToTime } from "@/utils/validate";
import Axios from "axios";
export default {
  props: {
    cameralistparam: {
      type: Object,
      default: () => { }
    }
  },
  components: { CameraModule },
  data () {
    return {
      controlFlag: false,
      cameraInfo: {
        base: ''
      },
      showCameraBox: false,
      // 当前摄像头id
      cameraId: "",
      // 当前选中摄像头的详细信息
      cameraInfoList: {},
      // 摄像头列表
      cameraList: [],
      // 网页播放地址
      cameraurl: "",
      startUp: "",
      openSourceMark: "",
      cloudBaseurl: 'https://tdiot.cloud/'
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {
    cameralistparam (newval) {
      this.getcameralist(newval);
    }
  },
  mounted () { },
  beforeDestroy () {
    // 离开界面销毁预热
    this.closePreheat();
  },
  methods: {
    closeDialog () {
      this.showCameraBox = false;
      this.cameraInfo = {
        time: '',
        imgSrc: ''
      }
    },
    async queryPhotograph (isNormal) {
      let cameraTime = timestampToTime(new Date())
      VideoSurveillanceApi.photograph({ cameraId: this.cameraId }).then(async res => {
        console.log(res, '拍照数据')
        if (res.data) {
          let image = await inspectionElectronicPatrolApi.getPicture({ pictureUrl: escape(res.data.picUrl) })
          if (image.success) {
            this.cameraInfo = {
              time: cameraTime,
              imgSrc: res.data.picUrl ? res.data.picUrl : '',
              isNormal: isNormal,
              base: image.data
            }
            this.showCameraBox = true
          }
        }
      });
    },
    // 获取摄像头列表
    getcameralist (params) {
      this.cameraList = [];
      VideoSurveillanceApi.getcameraList({ fishPondId: params.fishPondId }).then(res => {
        // VideoSurveillanceApi.queryByFishPondId(params.fishPondId).then(res => {
        // console.log(res)
        if (res.data) {
          this.cameraList = res.data;
          console.log(res.data, '----')
          // 判断有无摄像头列表数据
          if (res.data[0]) {
            this.cameraId = res.data[0].iotCameraId;
            this.controlFlag = res.data[0].controlFlag
            this.getcamermes(this.cameraId);
          } else {
            // 无 传不存在的值
            this.getcamermes(-1);
          }
        }
      });
    },
    // 获取某个摄像头详细信息
    getcamermes (id) {
      this.$emit("nowcameraurl", "transCamera");
      this.cameraId = id;
      if (id !== -1) {
        this.getSame(id);
      }
    },
    // 封装获取摄像详细信息
    async getSame (id) {
      VideoSurveillanceApi.getcameramessage(id).then(res => {
        let data = res.data;
        this.cameraInfoList = data ? data.camera : {};
        this.setcameraurl();
        // const enc = new TextDecoder('utf-8');
        // let res2 = undefined
        // res.arrayBuffer().then(buffer => {
        //   res2 = JSON.parse(enc.decode(new Uint8Array(buffer))) || {};
        //   let data = res2.data;
        //   this.cameraInfoList = data?data.camera:{};
        //   this.setcameraurl();
        // })
      });
      this.$emit("nowcameraurl", "notCameraList");
    },
    // 封装预热销毁函数
    async closePreheat () {
      await Axios({
        method: "post",
        url: this.cloudBaseurl + "iotApplication/solarEnergyCamera/closePower",
        data: { openSourceMark: this.openSourceMark }
      }).then(
        res => {
          console.log(res);
        },
        err => {
          console.log(err);
        }
      );
    },
    // 查询剩余流量
    // async getcams(id) {
    //   let zhurl = `https://tdiot.cloud/iotApplication/cameraOperation/judgeFlow?cameraId=${id}`
    //   const { data: res } = await Axios({
    //     method: "get",
    //     url: zhurl
    //   });
    //   let data = res.data;
    //   if (res.success) {
    //     if (data.flowUse) {
    //       this.$message.success("剩余流量" + data.flowUse);
    //     }
    //     return data.basis;
    //   } else {
    //     this.$message.warning(res.message || "未知错误！！！");
    //     return false;
    //   }
    // },
    // 拼接摄像头地址
    setcameraurl () {
      // 判断有无摄像头
      if (this.cameraInfoList) {
        if (this.cameraInfoList.cameraInfo && this.cameraInfoList.cameraInfo.status === 1) {
          this.cameraurl = "";
          if (this.cameraInfoList) {
            this.cameraurl = `https://open.ys7.com/ezopen/h5/iframe?url=ezopen://open.ys7.com/${this.cameraInfoList["deviceSerial"]}/${this.cameraInfoList["channelNo"]}.hd.live&autoplay=1&accessToken=${this.cameraInfoList["accessToken"]}`;
          }
          this.$emit("nowcameraurl", this.cameraurl);
        } else {
          this.$message.warning("摄像头不在线！请稍后重试");
          this.$emit("nowcameraurl", "notCameraData");
          return;
        }
      } else {
        this.$emit("nowcameraurl", "notCameraList");
      }
    },
    // 开始控制
    async startcontrol (i) {
      let params = {
        cameraId: this.cameraId,
        direction: i,
        speed: 1
      };
      await VideoSurveillanceApi.controlcamera(params);
      setTimeout(() => {
        this.stopcontrol();
      }, 1000);
    },
    // 镜像翻转
    async mirrorcontrol () {
      let params = {
        cameraId: this.cameraId,
        direction: 2,
        speed: 1
      };
      await VideoSurveillanceApi.mirrorcamera(params);
      setTimeout(() => {
        this.stopcontrol();
      }, 1000);
    },
    // 停止控制
    stopcontrol () {
      let params = {
        cameraId: this.cameraId
      };
      VideoSurveillanceApi.stopcamera(params).then(res => {
        // console.log(res)
      });
    }
  }
};
</script>
<style lang="scss" scoped>
$height: 40px;

.uncontrolsty {
  width: 100%;
  height: calc(70% - 10px);
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;

  .uncontrolsty-line {
    color: #ccc;
    font-size: 30px;
    width: 30px;
    line-height: 50px;
  }
}

.allcameramain {
  height: 100%;
  width: 100%;
  background: #ffffff;
  position: relative;

  .shade {
    width: 100%;
    height: 100%;
    background: #ffffff;
    opacity: 0.5;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 100;
  }

  .shade1 {
    width: 100%;
    height: 60%;
    background: #ffffff;
    opacity: 0.5;
    position: absolute;
    bottom: 0;
    left: 0;
    z-index: 100;

  }
}


.camera-module {
  background: #008bccc2;
  padding: 10px 20px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  position: absolute;
  left: -300px;
  bottom: 10px;
  color: #ffffff;

  .title {
    font-size: 16px;
    color: #ffffff;
  }
}

.cameralistmain {
  width: 100%;
  height: 40%;
  display: flex;
  flex-flow: nowrap column;
  align-items: center;
  justify-content: center;

  .camera-title {
    width: 80%;
    line-height: 30px;
    margin: 0 auto;
    text-align: center;
    font-size: 18px;
  }

  .camera-zlist {
    width: 100%;
    height: calc(100% - 20px);
    z-index: 5;
    top: -10px;
    overflow: auto;

    .camera-item {
      cursor: pointer;
      width: 225px;
      padding: 0 20px;
      font-size: 20px;
      line-height: 40px;
      margin: 0 auto;
      color: #57A3E6;
      margin-bottom: 10px;
      border-radius: 6px;
      text-align: center;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      border: 2px solid #57A3E6;
    }

    .camera-item:nth-child(1) {
      margin-top: 20px;
    }

    .camera-item:hover {
      border: 2px solid #57A3E6;
      background: #75a7d3;
      color: #ffffff;
    }

    .beselect {
      // background-image: linear-gradient(to right, #46c357, #b3ec70, #46c357);
      border: 2px solid #57A3E6;
      background: #57A3E6;
      color: #ffffff;
    }
  }
}

.cameracontrol {
  margin-top: 10px;
  margin: 0 auto;
  color: #e3e3e3 !important;

  .control {
    width: 277px;
    height: 350px;
    margin: 0 auto;
    background-image: url("~@/assets/camera/ctrlBg.png");

    .keyList {
      width: 100%;
      height: 277px;

      .contro-m {
        // border: #46c357 1px solid;
        position: relative;
        width: 25%;
        height: 25%;
        text-align: center;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 40px;
        cursor: pointer;
      }

      .contro-m:hover {
        font-size: 48px;
      }

      .z-top {
        top: 10%;
        left: 37%;
      }

      .z-left {
        top: 12%;
        left: 11%;
      }

      .z-right {
        top: -13%;
        left: 63%;
      }

      .z-bottom {
        top: -10%;
        left: 37%;
      }

      .z-mirror {
        top: -63%;
        left: 37%;
      }
    }

    .focal-length {
      width: 100%;
      height: 73px;
      margin-bottom: 15px;
      // background: red;
      display: flex;
      justify-content: space-around;

      div {
        width: 50%;
        text-align: center;
        line-height: 65px;
        font-size: 20px;
        color: #ffffff;
        cursor: pointer;
      }

      div:hover {
        font-size: 22px;
      }

      .left {
        text-align: right;
        padding-right: 15px;
      }

      .right {
        text-align: left;
        padding-left: 15px;
      }
    }
  }


  .controne {
    width: 100%;
    height: 65%;
    background-image: url("~@/assets/camera/ctrlBg.png");
    background-repeat: no-repeat;
    background-size: 90%;
    background-position: center;
    position: relative;

    .contro-m {
      // border: #46c357 1px solid;
      position: relative;
      width: 25%;
      height: 25%;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 40px;
      cursor: pointer;
    }

    .contro-m:hover {
      font-size: 48px;
    }

    .z-top {
      top: 10%;
      left: 37%;
    }

    .z-left {
      top: 12%;
      left: 11%;
    }

    .z-right {
      top: -13%;
      left: 63%;
    }

    .z-bottom {
      top: -10%;
      left: 37%;
    }

    .z-mirror {
      top: -63%;
      left: 37%;
    }
  }

  .controtwo {
    width: 100%;
    display: flex;
    justify-content: space-around;

    .contro-left {
      width: 90px;
      height: 60px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformLeft.png");
      background-repeat: no-repeat;
      background-size: 100% 100%;
      background-position: center;
      padding-right: 10px;
      cursor: pointer;
      font-size: 16px;
    }

    .contro-left:hover {
      font-size: 18px;
      padding-left: 5px;
    }

    .contro-right {
      width: 90px;
      height: 60px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformRight.png");
      background-repeat: no-repeat;
      background-size: 100% 100%;
      background-position: center;
      padding-left: 10px;
      cursor: pointer;
      font-size: 16px;
    }

    .contro-right:hover {
      font-size: 18px;
      padding-left: 5px;
    }
  }

  .controthree {
    width: 100%;
    height: 17%;
    display: flex;
    justify-content: space-around;
    align-items: center;

    .greenbut {
      width: 80px;
      height: 60%;
      background-color: rgb(132, 180, 111);
      border-radius: 5px;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: 600;
      font-size: 18px;
      cursor: pointer;
    }

    .greenbut:hover {
      font-size: 24px;
    }
  }
}

.cameracontrol1 {
  width: 100%;
  height: calc(70% - 10px);
  margin-top: 10px;

  .controne {
    width: 100%;
    height: 65%;
    background-image: url("~@/assets/camera/ctrlBg.png");
    background-repeat: no-repeat;
    background-size: 90%;
    background-position: center;
    position: relative;
    mix-blend-mode: soft-light;

    .contro-m {
      // border: #46c357 1px solid;
      position: relative;
      width: 25%;
      height: 25%;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 40px;
      cursor: pointer;
    }

    .z-top {
      top: 10%;
      left: 37%;
    }

    .z-left {
      top: 12%;
      left: 11%;
    }

    .z-right {
      top: -13%;
      left: 63%;
    }

    .z-bottom {
      top: -10%;
      left: 37%;
    }

    .z-mirror {
      top: -63%;
      left: 37%;
    }
  }

  .controtwo {
    width: 100%;
    height: 15%;
    display: flex;
    justify-content: space-around;

    .contro-left {
      width: 48%;
      height: 100%;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformLeft.png");
      background-repeat: no-repeat;
      background-size: 70%;
      background-position: center;
      padding-right: 30px;
      mix-blend-mode: soft-light;
      cursor: pointer;
      font-size: 18px;
    }

    .contro-right {
      width: 48%;
      height: 100%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformRight.png");
      background-repeat: no-repeat;
      background-size: 70%;
      background-position: center;
      padding-left: 30px;
      mix-blend-mode: soft-light;
      cursor: pointer;
      font-size: 18px;
    }
  }

  .controthree {
    width: 100%;
    height: 17%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    mix-blend-mode: soft-light;

    .greenbut {
      width: 28%;
      height: 60%;
      background-color: rgb(132, 180, 111);
      border-radius: 5px;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: 600;
      font-size: 22px;
      cursor: pointer;
    }
  }
}

.zlistbtn {
  display: flex;
  justify-content: center;
  align-items: center;

  .btn {
    border: none !important;
    height: 100%;
  }
}
</style>
